import { watchEffect, ref } from "vue";
import { getTopCategoryAPI } from "@/apis/category";
import { useRoute } from "vue-router";

export function useCategory() {
    // 获取分类数据
    const categoryData = ref({});
    const route = useRoute();
    const getCategory = async (id = route.params.id) => {
        const res = await getTopCategoryAPI(id);
        categoryData.value = res.result;
    };

    //方法一
    watchEffect(() => {
        getCategory(route.params.id)
    })
    //方法二
    // onMounted(() => {
    //     getCategory()
    // })
    // onBeforeRouteUpdate((to) => {
    //   getCategory(to.params.id);
    // });
    return {
        categoryData
    };
}